package com.yunupmall.controller.ford;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunupmall.api.fordVO.ReturngoodsVO;
import com.yunupmall.service.ford.service.ReternGoodService;
import com.yunupmall.uits.ReadUtils;
import com.yunupmall.uits.Result;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

@Controller
@AllArgsConstructor
@RequestMapping("/reterngood")
public class ReternGoodController {
    private final ReternGoodService reternGoodService;
    private final Result result;
    @PostMapping("/save")
    public String saveData(@RequestParam(value = "refund",required = false)Integer refund,
                           @RequestParam(value = "cargoStatus",required = false) Integer cargoStatus,
                           @RequestParam(value = "reason",required = false) String reason,
                           @RequestParam(value = "remarks",required = false) String remarks,
                           @RequestParam(value = "orderId") Integer orderId){
         try {
             reternGoodService.saveData(refund,cargoStatus,reason,remarks,orderId);
             return "udai_order";
         }catch (Exception e) {
             return e.getMessage();
         }
     }

     @PostMapping("/page")
     @ResponseBody
     public Result<Page<ReturngoodsVO>> pageData(@RequestBody ReturngoodsVO returngoodsVO){
         try {
             return result.ok(reternGoodService.pageData(returngoodsVO));
         }catch (Exception e) {
             return result.error(e.getMessage());
         }
     }
     @PostMapping("/update")
     @ResponseBody
    public Result update(@RequestBody ReturngoodsVO returngoodsVO){
         try {
             return result.ok(reternGoodService.updateData(returngoodsVO));
         }catch (Exception e) {
             return result.error(e.getMessage());
         }
     }
    @GetMapping("/see")
    @ResponseBody
    public Result byid(@RequestParam String number){
        String host = "https://wuliu.market.alicloudapi.com";// 【1】请求地址 支持http 和 https 及 WEBSOCKET
        String path = "/kdi";  // 【2】后缀
        String appcode = "fc21b82ed4244f1a9ea58c7d1f913902"; // 【3】开通服务后 买家中心-查看AppCode
        String no = number;// 【4】请求参数，详见文档描述
        String urlSend = host + path + "?no=" + no;  // 【5】拼接请求链接
        try {
            URL url = new URL(urlSend);
            HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
            httpURLCon .setRequestProperty("Authorization", "APPCODE " + appcode);// 格式Authorization:APPCODE (中间是英文空格)
            int httpCode = httpURLCon.getResponseCode();
            if (httpCode == 200) {
                String json = ReadUtils.read(httpURLCon.getInputStream());
                JSONObject jsonObject = JSON.parseObject(json);
                System.out.printf(jsonObject.toString());
                return result.ok(jsonObject);
            } else {
                Map<String, List<String>> map = httpURLCon.getHeaderFields();
                String error = map.get("X-Ca-Error-Message").get(0);
                if (httpCode == 400 && error.equals("Invalid AppCode `not exists`")) {
                    return result.error("AppCode错误");
                } else if (httpCode == 400 && error.equals("Invalid Url")) {
                    return result.error("请求的 Method、Path 或者环境错误");
                } else if (httpCode == 400 && error.equals("Invalid Param Location")) {
                    return result.error("参数错误");
                } else if (httpCode == 403 && error.equals("Unauthorized")) {
                    return result.error("服务未被授权（或URL和Path不正确）");
                } else if (httpCode == 403 && error.equals("Quota Exhausted")) {
                    return result.error("套餐包次数用完");
                } else if (httpCode == 403 && error.equals("Api Market Subscription quota exhausted")) {
                    return result.error("套餐包次数用完，请续购套餐");
                } else {
                    return result.error("参数名错误 或 其他错误");
                }
            }

        } catch (MalformedURLException e) {
            return result.error("URL格式错误");
        } catch (UnknownHostException e) {
            return result.error("URL地址错误");
        } catch (Exception e) {
            return result.error(e.getMessage());
        }
    }

    @PostMapping("/complete")
    @ResponseBody
    public Result<String> complete(@RequestBody ReturngoodsVO returngoodsVO){
         try {
             return result.ok(reternGoodService.complete(returngoodsVO));
         }catch (Exception e) {
             return result.error(e.getMessage());
         }
    }
    @GetMapping("/del")
    @ResponseBody
    public Result<String> del(@RequestParam Integer id){
        try {
            return result.ok(reternGoodService.del(id));
        }catch (Exception e) {
            return result.error(e.getMessage());
        }
    }
}
